package com.ibeetl.admin.core.rbac.tree;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ibeetl.cms.entity.CmsCatalog;

public class CatalogItem  implements TreeItem{
	CmsCatalog sysCatalog = null;
	List<CatalogItem> children = new ArrayList<CatalogItem>();
	@JsonIgnore
	CatalogItem parent = null;
	String name;
	private Integer seq;
	public CatalogItem(CmsCatalog sysCatalog){
		this.sysCatalog = sysCatalog;
		this.name = sysCatalog!=null?sysCatalog.getName():null;
		this.seq = sysCatalog.getSeq();
	}
	
	
	public void setParent(CatalogItem parent){
		this.parent = parent;
		parent.children.add(this);
	}
	
	public List<CatalogItem> getChildren(){
		return this.children;
	}
	
	public Long getId(){
		return sysCatalog.getId();
	}
	
	public CmsCatalog getData(){
		return this.sysCatalog;
	}
	
	
	public void filter(Set<Long> allows){
		Iterator<CatalogItem> it = this.children.iterator();
		while(it.hasNext()){
			CatalogItem item = it.next();
			if(item.getChildren().size()==0&&!allows.contains(item.getData().getId())){
				it.remove();
			}
			
			if(item.getChildren().size()!=0){
				item.filter(allows);
				if(item.getChildren().size()==0){
					it.remove();
				}
			}
			
			
		
		}
		
	}
	
	
	/**
	 * 查找某个指定的子功能
	 * @param functionId
	 * @return
	 */
	public CatalogItem findChild(Long catalogId){
		if(sysCatalog.getId().equals(catalogId)){
			return this;
		}
		for(CatalogItem item:children){
			CatalogItem find = item.findChild(catalogId);
			if(find!=null){
				return find;
			}
		}
		return null;
		
	}
	
	/**
	 * 查找所有的子栏目
	 * @return
	 */
	public List<CatalogItem> findAllItem(){
		List<CatalogItem> all = new LinkedList<>();
		findAllChildItem(all,this);
		return all;
	}
	
	
	private void findAllChildItem(List<CatalogItem> all,CatalogItem parent){
		for(CatalogItem item:parent.children){
			all.add(item);
			findAllChildItem(all,item);
		}
		
	}
	


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((sysCatalog == null) ? 0 : sysCatalog.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CatalogItem other = (CatalogItem) obj;
		if (sysCatalog == null) {
			if (other.sysCatalog != null)
				return false;
		} else if (!sysCatalog.equals(other.sysCatalog))
			return false;
		return true;
	}


	public CatalogItem getParent() {
		return parent;
	}


	@Override
	public String toString() {
		return "CatalogItem [sysCatalog=" + sysCatalog.getName() + ","+this.children.size()+"]";
	}


	@Override
	public String getName() {
		return this.name;
	}


	public Integer getSeq() {
		if (this.seq == null) {
			this.seq = 0;
		}
		return seq;
	}


	public void setSeq(Integer seq) {
		this.seq = seq;
	}



	
	
	
	

}
